from time import sleep,time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver=webdriver.Chrome()

# 4、弹窗
# ## 警告弹窗
# driver.get("file:///E:/source/learn/SET-AutoTest/LearnAutoTest/alert.html")
# sleep(1)
# # 点击调起弹窗
# driver.find_element(By.CSS_SELECTOR,"body > div > button").click()
# ## 切换到弹窗
# alert = driver.switch_to.alert
# sleep(1)
# ## 确定/取消-只有一个按钮，所以这两个操作都可以进行确认
# # alert.accept()
# alert.dismiss()
# sleep(1)

# ## 确认弹窗
# driver.get("file:///E:/source/learn/SET-AutoTest/LearnAutoTest/confirm.html")
# driver.find_element(By.CSS_SELECTOR,"body > div > button").click()
# sleep(1)
# ## 切换到弹窗
# alert = driver.switch_to.alert
# ## 确定/取消-按钮会有区别
# alert.accept()
# # alert.dismiss()
# sleep(1)

# ## 提示弹窗---输入框
# driver.get("file:///E:/source/learn/SET-AutoTest/LearnAutoTest/prompt.html")
# # 点击调起弹窗
# driver.find_element(By.CSS_SELECTOR,"body > div > button").click()
# sleep(1)
# ## 输入文本+确定+取消
# alert = driver.switch_to.alert
# # 肉眼看不到在弹窗上输入的文本信息
# alert.send_keys("你好世界")
# sleep(1)
# alert.accept()
# # alert.dismiss()
# sleep(1)

# 5、等待
# ## 强制等待-等待时间太长了
# driver.find_element(By.CSS_SELECTOR,"#chat-textarea").send_keys("你好")
# sleep(2)
# driver.find_element(By.CSS_SELECTOR,"#chat-submit-button").click()
# sleep(2)

# ## 隐式等待-作用域为全局，无论写在哪里-只能等页面元素的内容
# #  如果存在并找到，就继续执行，否则报错
# driver.implicitly_wait(3)
# driver.find_element(By.CSS_SELECTOR,"#chat-textarea").send_keys("你好")
# driver.find_element(By.CSS_SELECTOR,"#chat-submit-button").click()

# ## 显式等待，作用于某一行代码
# driver.get("https://www.baidu.com/")
# wait=WebDriverWait(driver,3)
# # visibility-查找元素是否可见，传参只有一个，因此加一个括号将其变成一个元素
# wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"#chat-input-extension > div > div > div:nth-child(2)")))
#
# # 检查是否出现弹窗-解决隐式等待无法处理的问题
# driver.get("file:///E:/source/learn/SET-AutoTest/LearnAutoTest/confirm.html")
# # 切换到弹窗标签
# curHandle=driver.current_window_handle
# allHandles=driver.window_handles
# for handle in allHandles:
#     if handle!=curHandle:
#         driver.switch_to.window(handle)
# driver.find_element(By.CSS_SELECTOR,"body > div > button").click()
# # 等待弹窗是否出现，出现后再继续执行后续操作
# wait.until(EC.alert_is_present())
# driver.switch_to.alert.accept()
# sleep(1)

# 不能同时使用隐式等待和显式等待，否则等待时间不可预测
# 隐式等待设置为10s，显⽰等待设置为15s，等待了11.132206678390503秒
driver.implicitly_wait(5)
wait=WebDriverWait(driver,10)
start=time()  # 保存开始时间（时间戳）
end=0  # 保存结束时间（时间戳）
try:
    res=wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="csaitab123"]/img')))
except TimeoutException:
    end=time()
    print("no such element")
print(end-start)

driver.quit()